package org.smartstruts.config;

import java.util.HashMap;
import java.util.Map;

/**
 * 配置对象类<br>
 * 该类对应了smart-struts.xml这个配置文件中<smart-struts>的设置<br>
 * 意为一个该类的对象，就对应了一个配置文件的信息<br>
 * 
 * @author Wangpeihu
 * @team [Never give up]
 * @since JDK1.6(建议)
 */
public class ModuleConfig {

	/** 封装有可能出现的form类 */
	private Map<String, FormBeanConfig> formBeans = new HashMap<String, FormBeanConfig>();

	/** 封装有可能出现的action类 */
	private Map<String, ActionConfig> actions = new HashMap<String, ActionConfig>();

	/** 封装有可能出现的forward类 */
	private Map<String, ForwardConfig> globalForwards = new HashMap<String, ForwardConfig>();

	/**
	 * 添加一个formBeanConfig对象到本类的formBeans属性中
	 * 
	 * @param formBeanConfig
	 */
	public void addFormBeanConfig(FormBeanConfig formBeanConfig) {
		formBeans.put(formBeanConfig.getName(), formBeanConfig);
	}

	/**
	 * 得到一个FormBeanConfig对象
	 * 
	 * @param name
	 * @return
	 */
	public FormBeanConfig findFormBeanConfig(String name) {
		if (formBeans.containsKey(name)) {
			return formBeans.get(name);
		}
		return null;
	}

	/**
	 * 添加一个forwardConfig对象到本类的globalForwards属性里
	 * 
	 * @param forwardCfg
	 */
	public void addForwardConfig(ForwardConfig forwardCfg) {
		globalForwards.put(forwardCfg.getName(), forwardCfg);
	}

	/**
	 * 得到一个ForwardConfig对象
	 * 
	 * @param name
	 * @return
	 */
	public ForwardConfig findForward(String name) {
		if (globalForwards.containsKey(name)) {
			return globalForwards.get(name);
		}
		return null;
	}

	/**
	 * 添加一个ForwardConfig对象
	 * 
	 * @param actionCfg
	 */
	public void addActionConfig(ActionConfig actionCfg) {
		actions.put(actionCfg.getPath(), actionCfg);
	}

	/**
	 * 得到一个ActionConfig对象
	 * 
	 * @param path
	 * @return
	 */
	public ActionConfig findActionConfig(String path) {
		if (actions.containsKey(path)) {
			return actions.get(path);
		}
		return null;
	}

	@Override
	public String toString() {
		StringBuffer buffer = new StringBuffer();
		buffer.append("-----global-forwards-----\n");
		for (Object value : globalForwards.values()) {
			buffer.append(value + "\n");
		}
		buffer.append("-----action-mappings-----\n");
		for (Object value : actions.values()) {
			buffer.append(value + "\n");
		}
		return buffer.toString();
	}
}
